package cn.com.dstz.agent.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.admin.constants.Constants;
import cn.com.dstz.utils.Common;

public abstract class AgentConfig{

	public static final String SELF = "self";	// 自己
	public static final String ALL = "all"; 	// 所有
	public static final String DEFINED = "defined";
	
	public static final String CONFIG_ANALYSIS = "config_analysis";
	public static final String CONFIG_ADMIN = "config_admin";
	public static final String CONFIG_USER = "config_user";
	public static final String CONFIG_NOTICE = "config_notice";
	public static final String CONFIG_CATEGORY = "config_category";
	public static final String CONFIG_TRADE_OPEN = "config_trade_open";
	public static final String CONFIG_TRADE_CLOSE = "config_trade_close";
	public static final String CONFIG_ACCOUNT = "config_account";
	public static final String CONFIG_AGENT = "config_agent";
	
	protected Map< String, Object > valueMap;
	protected Map< String, Object > admin;
	protected int count;
	protected String key;
	protected String [ ] groupIds;	protected String returnValue;
	
	
	public AgentConfig( ){}
	
	public AgentConfig( Map< String, Object > admin, int count, String key ){
		this.admin = admin;
		this.count = count;
		this.key = key;
		this.valueMap = new HashMap< String, Object >( );
		initConfigValue( );
	}
	
	public abstract String getSQL( );
	
	public String getDefinedSQL( ){
		StringBuffer buffer = new StringBuffer( );
		buffer.append( "(" );
		for( int i = 0; i < groupIds.length; i++ ){
			String id = groupIds[ i ];
			
			buffer.append( " groupid=:g" + id );
			valueMap.put( "g" + id, id );
			
			if( i < groupIds.length - 1 ){
				buffer.append( " or " );
			}
		}
		buffer.append( ")" );
		return buffer.toString( );
		
	}
	
	public boolean hasConfig(  ){
		Object hasConfig =  admin.get( Constants.CONFIG_NAME );
		if( hasConfig == null ){
			return false;
		}
		return ( Boolean ) hasConfig;
	}
	
	public void initConfigValue( ){
		
		if( !hasConfig( )){
			this.returnValue = SELF;
		}else{
			Map< String, Object > config = Common.convert( admin.get( Constants.AGENT_CONFIG_IN_SESSION ) );
			Object value = config.get( key ); 
			
			if( value == null || StringUtils.isBlank( value.toString( ) )){
				this.returnValue = SELF;
			}else{
				String [] splitGroups = StringUtils.split( value.toString( ), "," );
				int len = splitGroups.length;
				if( len == 0 ){
					returnValue = SELF;
				}else if( len == count ){
					this.returnValue = ALL;
				}else{
					this.groupIds = splitGroups;
					this.returnValue = DEFINED;
				}
			}
		}
	}

	public Map< String, Object > getAdmin( ) {
		return admin;
	}

	public void setAdmin( Map< String, Object > admin ) {
		this.admin = admin;
	}

	public int getCount( ) {
		return count;
	}

	public void setCount( int count ) {
		this.count = count;
	}

	public String getKey( ) {
		return key;
	}

	public void setKey( String key ) {
		this.key = key;
	}

	public String [ ] getGroupIds( ) {
		return groupIds;
	}

	public void setGroupIds( String [ ] groupIds ) {
		this.groupIds = groupIds;
	}

	public String getReturnValue( ) {
		return returnValue;
	}

	public void setReturnValue( String returnValue ) {
		this.returnValue = returnValue;
	}

	public Map< String, Object > getValueMap( ) {
		return valueMap;
	}

	public void setValueMap( Map< String, Object > valueMap ) {
		this.valueMap = valueMap;
	}
	
	
}
